﻿using AutoMapper;
using MediatR;
using Schedulingclasses.Api.DTO;
using Schedulingclasses.Api.User.Command;
using Schedulingclasses.Domain;
using Schedulingclasses.ErrorCode;
using Schedulingclasses.Infrastructure;
using System.Globalization;

namespace Schedulingclasses.Api.User.Handler
{
    public class SreachRegionHandler : IRequestHandler<SreachRegionCommand, ApiResult<List<SreachRegionDto>>>
    {
        private readonly IBaseRepository<RegionModel> region;
        private readonly IMapper mapper;

        public SreachRegionHandler(IBaseRepository<RegionModel> region,IMapper mapper)
        {
            this.region = region;
            this.mapper = mapper;
        }

        public async Task<ApiResult<List<SreachRegionDto>>> Handle(SreachRegionCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<SreachRegionDto>>();

            res.code = ApiREunm.Fail;
            res.msg = "级联显示成功";
            res.data = GetSreachRegion(0);

            return res;
        }

        private List<SreachRegionDto> GetSreachRegion(int v)
        {
            var inf = region.Getlist().Where(x => x.Superior.Equals(v));

            var model=mapper.Map<List<SreachRegionDto>>(inf.ToList());

            foreach (var item in model)
            {
                item.children = GetSreachRegion(item.RegionId);
            }

            return model;


        }
    }
}
